import nltk
import csv
import re
import math
class Features(object):
    def __init__(self, trainfileName, testfileName,  trainPOSfile, testPOSfile):
        self.trainfileName=trainfileName
        self.testfileName=testfileName
        self.testPOSfile=testPOSfile
        self.trainPOSfile=trainPOSfile
        self.headings=["AIM", "METHOD", "RESULTS", "CONCLUSION"]
                   
 
    
    def createPOSTrainfileFeatures(self, stopWordFile, outputFile):
        pattern1=re.compile('[a-zA-Z]')
        stopFile = open(stopWordFile)
        stopWords = [line.strip().upper() for line in stopFile.readlines()]
        stopFile.close()
        resultFile = open(outputFile, 'w')        
        spamReader = csv.reader(open(self.trainfileName, 'rb'), delimiter=',')
        posFile=open(self.trainPOSfile)
        count = 0
        heading = ""
        temp_heading=""
        sentList=[]
        structBool=False
        for line in spamReader:            
            count += 1
            resultText = ""
            wordCount = 0
            if line[0]=="1":
                temp_heading=line[1]
            if line[3]=="1" and len(sentList)>0:
                indList=[]
                if not structBool:
                    #print "Entered and Checking!"
                    for i in range(1,5):                        
                        indList.append(math.ceil(float(len(sentList)*i/4)))
                for i in range(0,len(sentList)):
                    sentNorm=float(i+1)/len(sentList)
                    revInd=len(sentList)-i 
                    if not structBool:
                        heading=self.getDynamicHeading(i,indList, sentList)
                    else:
                        heading=sentList[i][1]                                                                    
                    resultFile.write(str(sentNorm)+","+str(revInd)+","+sentList[i][0]+","+heading+"\n")
                    #resultFile.write(str(sentNorm)+","+sentList[i][0]+","+heading+"\n")
                sentList=[]                                   
            if (count > 1):
                if ((count - 1) % 6 == 0):                                
                    posWords=posFile.readline().strip().split()                     
                    for word_pos in posWords:                       
                        #spl=word_pos.split("_")                       
                        #if word_pos.upper() not in stopWords and len(spl[0])>2 and pattern1.match(spl[0]):
                        if word_pos not in stopWords and len(word_pos)>1 and pattern1.match(word_pos):
                            wordCount += 1                                                             
                    sentPosition = line[3]
                    #resultFile.write(str(sentPosition) + "," + str(sentCount) + "," + heading + "\n")
                    if sentPosition=="1":
                        if line[4].isupper():
                            structBool=True                            
                        else:
                            structBool=False
                            #print "StructBool FALSE", line[4]
                    if structBool and line[4].isupper():
                        heading = self.modifyHeading(line[4], "")                                            
                        sentList.append([sentPosition + ","+str(wordCount),"4"])
                    elif structBool and not line[4].isupper():
                        sentList.append([sentPosition + ","+str(wordCount),heading])
                    elif not structBool:
                         sentList.append([sentPosition + ","+str(wordCount),temp_heading])        
        if len(sentList)>0:
            indList=[]
            if not structBool:
                #print "Entered and Checking!"
                for i in range(1,5):                        
                    indList.append(math.ceil(float(len(sentList)*i/4)))
            for i in range(0,len(sentList)):
                sentNorm=float(i+1)/len(sentList)
                revInd=len(sentList)-i                
                if not structBool:
                    heading=self.getDynamicHeading(i,indList, sentList)
                else:
                    heading=sentList[i][1]                                                                    
                resultFile.write(str(sentNorm)+","+str(revInd)+","+sentList[i][0]+","+heading+"\n")
                #resultFile.write(str(sentNorm)+","+sentList[i][0]+","+heading+"\n")
        posFile.close()
        resultFile.close()
        
    def createPOSTestfileFeatures(self, stopWordFile, outputFile):
        pattern1=re.compile('[a-zA-Z]')
        stopFile = open(stopWordFile)
        stopWords = [line.strip().upper() for line in stopFile.readlines()]
        stopFile.close()
        resultFile = open(outputFile, 'w')        
        spamReader = csv.reader(open(self.testfileName, 'rb'), delimiter=',')
        posFile=open(self.testPOSfile)
        count = 0
        heading = ""
        sentList=[]
        structBool=False
        temp_heading=""
        for line in spamReader:            
            count += 1
            resultText = ""
            wordCount = 0          
            if line[2]=="1" and len(sentList)>0:
                #print line[3], structBool
                indList=[]
                if not structBool:
                    #print "Entered and checking!"
                    for i in range(1,5):
                        indList.append(math.ceil(float(len(sentList)*i/4)))
                for i in range(0,len(sentList)):
                    sentNorm=float(i+1)/len(sentList)
                    revInd=len(sentList)-i 
                    if not structBool:
                        heading=self.getDynamicHeading(i,indList,sentList)
                        #print heading
                    else:
                        heading=sentList[i][1]                                                                    
                    resultFile.write(str(sentNorm)+","+str(revInd)+","+sentList[i][0]+","+heading+"\n")
                    #resultFile.write(str(sentNorm)+","+sentList[i][0]+","+heading+"\n")
                sentList=[]                                   
            if (count > 1):
                if ((count - 1) % 6 == 0):                                
                    posWords=posFile.readline().strip().split()                     
                    for word_pos in posWords:                       
                        #spl=word_pos.split("_")                       
                        #if word_pos.upper() not in stopWords and len(spl[0])>2 and pattern1.match(spl[0]):
                        if word_pos not in stopWords and len(word_pos)>1  and pattern1.match(word_pos):
                            wordCount += 1                                                             
                    sentPosition = line[2]
                    #resultFile.write(str(sentPosition) + "," + str(sentCount) + "," + heading + "\n")
                    if sentPosition=="1":
                        if line[3].isupper():
                            structBool=True                            
                        else:
                            structBool=False
                            #print "StructBool FALSE", line[3]
                    if structBool and line[3].isupper():
                        heading = self.modifyHeading(line[3], "")                                            
                        sentList.append([sentPosition + ","+str(wordCount),"4"])
                    elif structBool and not line[3].isupper():
                        sentList.append([sentPosition + ","+str(wordCount),heading])
                    elif not structBool:
                         sentList.append([sentPosition + ","+str(wordCount),""])
        #print count,"no.of lines"
        if len(sentList)>0:
            indList=[]
            if not structBool:
                #print "Entered and Checking!"
                for i in range(1,5):                        
                    indList.append(math.ceil(float(len(sentList)*i/4)))
            for i in range(0,len(sentList)):
                sentNorm=float(i+1)/len(sentList)
                revInd=len(sentList)-i 
                if not structBool:
                    heading=self.getDynamicHeading(i,indList,sentList)
                else:
                    heading=sentList[i][1]                                                                    
                resultFile.write(str(sentNorm)+","+str(revInd)+","+sentList[i][0]+","+heading+"\n")
                #resultFile.write(str(sentNorm)+","+sentList[i][0]+","+heading+"\n")
        posFile.close()
        resultFile.close()
            
    def getDynamicHeading(self, index, indList,sentList):
        temp_heading=sentList[index][1]
        if temp_heading=="other":
            return "4"
        if temp_heading=="background":
            return "0"
        if temp_heading=="intervention" or temp_heading=="population" or temp_heading=="study design":
            return "1"
        if temp_heading=="outcome":
            return "2"
        if len(sentList)<=4:
            return str(index)
        if index<=indList[0]:
            return "0"
        if index<=indList[1]:
            return "1"
        if index<=indList[2]:
            return "2"
        if index<=indList[3]:
            return "3"
            
    def modifyHeading(self, headingWord, temp_heading): 
        if temp_heading=="other":
            headingWord="4"
            return headingWord
        if headingWord.rfind("OBJECT")>=0 or headingWord.rfind("PRINCIPLE")>=0 or headingWord.rfind("RATIONALE")>=0 or headingWord.rfind("CONTEXT")>=0 or headingWord.rfind("ABSTRACT")>=0 or headingWord.rfind("QUESTION")>=0 or headingWord.rfind("BACKGROUND")>=0 or headingWord.rfind("AIM")>=0 or headingWord.rfind("PURPOSE") >=0 or headingWord.rfind("INTRODUCTION")>=0 or headingWord.rfind("INDICATION")>=0 or headingWord.rfind("CONTRADICTION")>=0:
            headingWord= "0" #self.headings[0]
        elif headingWord.rfind("DEVELOPMENT") >=0 or headingWord.rfind("PRESENTATION") >=0 or  headingWord.rfind("INTERVENTION") >=0 or headingWord.rfind("OPTION")>=0 or headingWord.rfind("VALUE")>=0 or headingWord.rfind("METHOD") >=0 or headingWord.rfind("PREDICTORS") >=0 or headingWord.rfind("FEATURE") >=0 or headingWord.rfind("POPULATION") >=0 or headingWord.rfind("SAMPLE") >=0 or headingWord.rfind("EXTRACTION") >=0 or headingWord.rfind("SOURCE") >=0 or headingWord.rfind("SELECTION") >=0 or headingWord.rfind("PARTICIPANT") >=0 or  headingWord.rfind("SETTING") >=0 or headingWord.rfind("DESIGN") >=0 or headingWord.rfind("SUBJECT") >=0 or headingWord.rfind("STRATEGY")>=0 or headingWord.rfind("CRITERIA")>=0 or headingWord.rfind("PATIENT")>=0:
           headingWord = "1" #self.headings[1]
        elif headingWord.rfind("MEASURE") >=0 or headingWord.rfind("RESULT") >=0  or  headingWord.rfind("LIMITATIONS")>=0 or headingWord.rfind("EVIDENCE") >=0 or headingWord.rfind("SIGNIFICANCE") >=0  or  headingWord.rfind("REPORT") >=0 or headingWord.rfind("SYNTHESIS")>=0 or headingWord.rfind("FINDING") >=0 or headingWord.rfind("OUTCOME") >=0 or headingWord.rfind("TECHNIQUE") >=0 or headingWord.rfind("ANALYSIS") >=0:
           headingWord = "2" #self.headings[2]
        elif headingWord.rfind("DISCUSSION")>=0  or headingWord.rfind("AUTHOR")>=0 or headingWord.rfind("REVIEW")>=0 or headingWord.rfind("SUMMARY")>=0 or headingWord.rfind("BENEFIT") >=0 or headingWord.rfind("ISSUE") >=0 or  headingWord.rfind("RECOMMENDATION")>=0 or headingWord.rfind("CONCLUSION")>=0 or headingWord.rfind("INTERPRETATION")>=0 or headingWord.rfind("IMPLICATIONS")>=0 or headingWord.rfind("RELEVANCE")>=0:
            headingWord = "3" #self.headings[3]
        else:
             print headingWord+"\n"        
        return headingWord
    
    def joinFeatures(self, fileInp1, fileInp2, outFile):
        file1=open(fileInp1)
        file2=open(fileInp2)
        file3=open(outFile,'w')
        line1=file1.readline().strip()
        while line1!="" and line1!=None:
            line2=file2.readline().strip()
            file3.write(line1+line2+"\n")
            #print line1+line2+"\n"
            line1=file1.readline().strip()
    #break
        file3.close()
        file1.close()
        file2.close()
    
